<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/05/24
 * Time: 上午11:45
 */
class KingGlorySupport extends Eloquent
{
    protected $primaryKey = 'id';
    protected $table = "kingglory_support";

    public static function insertcount($team_id, $user_id, $anchors, $source, $type){
        $is_new = KingGloryUser::where("user_id",$user_id)->first();
        $bbr=new KingGlorySupport();
        if(is_null($is_new)){
            //判断用户是否领取新人礼
            $coupon = Coupons::getNewUserRegister(Config::get('config.activity_id'),$response); //查询关联优惠券ID
            $NewCoupon = Coupons::isCoupons($coupon[0]->coupon_base_info_id,$user_id);
            //已领取新人礼，判断领取日期和当前日期是否一致，一致即为新用户，不一致即为老用户
            if(!is_null($NewCoupon)){
                $get_time = explode(' ',$NewCoupon->get_time)[0];   //获取领取时间
                $new_time = date("Y-m-d",time());//获取当前时间
                $user_phone = Users::where("id",$user_id)->first();
                $new_user = new KingGloryUser();
                $new_user->user_id = $user_id;
                $new_user->type = '1';
                $new_user->phone = $user_phone['phone'];
                $new_user->created_at = date("Y-m-d H:i:s",time());
                $new_user->updated_at = date("Y-m-d H:i:s",time());
                $bbr->phone=$user_phone['phone'];
                if($get_time == $new_time){
                    $new_user->new = '新用户';
                    $bbr->is_new='新用户';
                }else{
                    $new_user->new = '老用户';
                    $bbr->is_new='老用户';
                }
                $new_user->save();
            }
        }else{
            $bbr->phone=$is_new['phone'];
            $bbr->is_new=$is_new['new'];
        }
        $bbr->team_id=$team_id;
        $bbr->user_id=$user_id;
        $bbr->anchors=$anchors;
        $bbr->source=$source;
        $bbr->type=$type;
        $bbr->created_at=date("Y-m-d H:i:s",time());
        $bbr->updated_at=date("Y-m-d H:i:s",time());
        $bbr->save();
        $team = KingGlorySupportCount::where("team_id",$team_id)->select("team_id","num")->first();
        return $team;
    }


}